<?php
/*
 * Author :pandiam
 */
require_once ('Include/css_head.php');
require_once ('Include/header.php');
require_once ('Include/functions.php');
require_once ('Include/editCategoryHelper.php');
require_once ('../BL/BL.php');
require_once ('../Frameworks/Objects/category.php');
require_once ('../Frameworks/Objects/user.php');

if (!isset($_SESSION['uid']) || $_SESSION['uid'] == -1) {
    UI_registerError('msg_warning', "You are not logged in.");
    header('Location: login.php');
    exit(0);
}

$attrbs = $mods = $users = $irrelev = "";

if ( isset($_GET['id']) && !is_null($_GET['id']) ) {
    if ( !ctype_digit((string)$_GET['id']) ) {
        UI_registerError('msg_error', "Invalid category request");
        header("location: index.php");
        exit(0);
    }
    $catid = $_GET['id'];
    $attrbs = getCategory();
    $mods = getCategoryModerators();
    $users = getInvitedUsers() ;
    $irrelev = getIrrelevantUsers();
}
else
{
    UI_registerError('msg_warning', "You haven't selected a category to edit!");
    header("location: index.php");
    exit(0);
}

if(isset($_SESSION['error1']) && isset($_SESSION['error2'])&& isset($_SESSION['error3']) && isset($_SESSION['error4'])){
    $catid = $_GET['id'];
    $attrbs = $_SESSION['error1'];
    $mods = $_SESSION['error2'];
    $users = $_SESSION['error3'];
    $irrelev = $_SESSION['error4'];
    unset($_SESSION['error1']);
    unset($_SESSION['error2']);
    unset($_SESSION['error3']);
    unset($_SESSION['error4']);
}

if ($catid != -1) {
    $category = BL_getCategory($catid);
?>
    <script type="text/javascript" src="js/editCategoryLib.js"></script>

    <form name="editCategory" action="editCategoryDo.php?id=<?php echo $catid?>" method="post">
        <input type="hidden" name="attrs" />
        <input type="hidden" name="mods" />
        <input type="hidden" name="users" />
        <input type="hidden" name="irrelev"/>
    <div id="complEdit">
    <div id="editCat">
         <h3>Category info</h3>
        <div style="overflow:auto; max-height:400px;">
            <table style="margin-bottom:10px;">
                <tr>
                    <td align="right">Category name<font color="red">*</font> :</td>
                    <td><input style="height:20px" type="text" id="catname" maxlength="30" size="30" value="<?php echo $category->GetName(); ?>"/></td>
                </tr>
                <tr>
                    <td align="right">Category type<font color="red">*</font> :</td>
                    <td align="left">
                        <select id="catType"  name="catType">
                           <option value="public" <?php if ($category->isPrivate() == 0) echo "selected=true"; ?> >Public</option>
                           <option value="private" <?php if ($category->isPrivate() == 1) echo "selected=true"; ?>  >Private</option>
                        </select>
                    </td>
                </tr>
            </table>
        </div>
        <div id="attribute">
                <h3 style="margin-top:20px">Insert Attribute</h3>
                <table>
                    <tr>
                        <td align="right">Attribute name<font color="red">*</font> :</td>
                        <td><input style="height:20px" type="text" id="attrname" maxlength="30" size="30"/></td>
                    </tr>
                    <tr>
                        <td align="right">Attribute type<font color="red">*</font> :</td>
                        <td align="center">
                            <input type="radio" name="type" id="meausure" value="measurable" onchange="javascript:toggle(1)" checked="checked" />Measurable
                            <input type="radio" name="type" id="unmeausure" value="unmeasurable" onchange="javascript:toggle(2)" />Un-Measurable
                        </td>

                    </tr>
                    <tr align="center">
                          <td></td><td><input type="radio" name="type" id="uncomparable" value="uncomparable" onchange="javascript:toggle(2)" />Un-Comparable</td>
                    </tr>
                <tr  id="hide_this" style="visibility:visible">
                    <td align="right">Best Value<font color="red">*</font> :</td>
                        <td>
                            <select id="best" onchange="javascript:toggleSelect()">
                            <option value="min">Min</option>
                            <option value="max">Max</option>
                            <option value="close_to">Close To</option>
                            </select>
                            <input style="visibility:hidden; height:20px" type="text" id="close_to" name="close_to_val" maxlength="30" size="10"/>
                        </td>
                </tr>
                </table>
             <a style="margin:0 0 0 40%" id="add_new_attr" href="javascript:keepAttribute()">Add new</a><br/><br/>
            </div>
            <div id="mods">
            <h3 style="margin-top:20px">Add Moderator</h3>
                <table>
                    <tr>
                        <td align="right">Username<font color="red">*</font> :</td>
                        <td><input id="moderator" type="text"/></td>
                    </tr>
                </table>
                <a style="margin:0 0 0 40%" id="add_new_attr" href="javascript:keepModerator()">Add new</a><br/>
            </div>
            <div id="users">
            <h3 style="margin-top:20px">Invite Users</h3>
                <table>
                    <tr>
                        <td align="right">Username<font color="red">*</font> :</td>
                        <td><input id="user" type="text"/></td>
                    </tr>
                </table>
                <a style="margin:0 0 0 40%" id="add_new_attr" href="javascript:keepUser()">Add new</a><br/>
            </div>
            <input type="button" onclick="javascript:passData()" value="submit"/>
    </div>
    </form>
    <div id="attrList">
        <h3>Attribute List</h3>
        <div id="attrCat" style="overflow:auto; max-height:400px; ">
        </div>
    </div>

    <div id="dataLists">
        <h3>Moderators</h3>
        <div id="attrCat2" style="overflow:auto; max-height:400px;">
        </div>
    </div>

    <div id="dataLists">
        <h3>Invited Users</h3>
        <div id="attrCat3" style="overflow:auto; max-height:400px;">
        </div>
    </div>
    </div>


<?php
    echo "<script type=\"text/javascript\">";
    if($attrbs != ""){
        echo "convert( \"".$attrbs."\");";
        echo "changeContent(\"delete\");";
    }

    if($mods != ""){
        echo "convertMods( \"".$mods."\");";
        echo "changeContentMods(\"delete\");";
    }

    if($users != ""){
        echo "convertUsers( \"".$users."\");";
        echo "changeContentUser(\"delete\");";
    }

    echo "var data=\"".$irrelev."\";";
    echo "var data2 = data.split(\",\");";
    echo "autocomplete();";
    echo "</script>";
}
require_once ('Include/footer.php');
?>